package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.First;
import com.example.entity.FirstVO;
import com.example.entity.Outcome;
import com.example.entity.Second;
import com.example.mapper.FirstMapper;
import com.example.mapper.OutcomeMapper;
import com.example.mapper.SecondMapper;
import com.example.service.ISecondService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 第二标签表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-11-11
 */
@RequiredArgsConstructor
@Service
public class SecondServiceImpl extends ServiceImpl<SecondMapper, Second> implements ISecondService {

    private final OutcomeMapper outcomeMapper;

    private final SecondMapper secondMapper;

    private final FirstMapper firstMapper;

    @Override
    public void delete(List<Long> ids) throws Exception {
        for (Long id : ids) {
            List<Outcome> outcomes = outcomeMapper.selectList(new LambdaQueryWrapper<Outcome>()
                    .eq(Outcome::getTypeKey2, id));
            if (outcomes != null && outcomes.size() > 0){
                throw new Exception("标签仍存在绑定收入数据，不能批量删除");
            }
            secondMapper.deleteById(id);
        }
    }

    @Override
    public List<FirstVO> select() {
        List<FirstVO> list = new ArrayList<>();
        List<First> firsts = firstMapper.selectList(new LambdaQueryWrapper<First>()
                .eq(First::getJudge, 1));
        for (First first : firsts) {
            List<Second> seconds = secondMapper.selectList(new LambdaQueryWrapper<Second>()
                    .eq(Second::getTypeKey, first.getId()));
            FirstVO firstVO = new FirstVO();
            BeanUtils.copyProperties(first,firstVO);
            firstVO.setSecondList(seconds);
            list.add(firstVO);
        }
        return list;
    }
}
